#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QListWidgetItem>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initplayer();
    connect(player,&QMediaPlayer::positionChanged,this,&MainWindow::on_playerPositionChanged);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initplayer()
{
    player = new QMediaPlayer();
    playlist = new QMediaPlaylist();
    player->setPlaylist(playlist);
    playlist->setPlaybackMode(QMediaPlaylist::Loop);//循环

    connect(playlist, &QMediaPlaylist::currentIndexChanged,
                this, &MainWindow::on_playlistIndexChanged);
}

void MainWindow::addMusic()
{
    for(auto file:musicFilses)
    {
        playlist->addMedia(QUrl::fromLocalFile(path+"/"+file));//拼接路径
    }
    playlist->setCurrentIndex(0);
   // player->play();
}


void MainWindow::on_pushButton_look_clicked()
{
    path = QFileDialog::getExistingDirectory(this,"选择音乐文件","E:/music");
    QDir dir(path,"*.mp3");
    musicFilses = dir.entryList();
    ui->listWidget->clear();
    ui->listWidget->addItems(musicFilses);

    addMusic();//触发添加歌曲路径到音乐播放列表
}

void MainWindow::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    int i = ui->listWidget->currentRow();
    playlist->setCurrentIndex(i);
    player->play();
    ui->label_musicName->setText(musicFilses[i]);//显示歌名
}

void MainWindow::on_playerPositionChanged(qint64 position)
{
    qint64 duration = player->duration();//获取歌曲总时长
    //qint64 pos = player->position();
    QString time = QString("%1:%2/%3:%4").arg(position/1000/60,2,10,QChar('0'))//第一个：内容；2:占位;10:进制;第四个：补位
            .arg(position/1000%60,2,10,QChar('0'))
            .arg(duration/1000/60,2,10,QChar('0'))
            .arg(duration/1000%60,2,10,QChar('0'));
    ui->label_time->setText(time);
    ui->horizontalSlider->setMaximum(duration);
    ui->horizontalSlider->setValue(position);
}

void MainWindow::on_horizontalSlider_sliderReleased()
{
    qint64 value = ui->horizontalSlider->value();//获取滑块位置
    player->setPosition(value);
}

void MainWindow::on_pushButton_pre_clicked()
{
    playlist->previous();
    player->play();
    int index = playlist->currentIndex();//获取当前音乐
    ui->label_musicName->setText(musicFilses[index]);//显示歌名
}

void MainWindow::on_pushButton_change_clicked()//播放/暂停
{
    if(player->state() == QMediaPlayer::PlayingState)//播放状态
    {
        player->pause();//暂停状态
    }
    else if(player->state() == QMediaPlayer::PausedState)//暂停状态
    {
        player->play();//播放状态
    }
}

void MainWindow::on_pushButton_next_clicked()
{
    playlist->next();
    player->play();
    int index = playlist->currentIndex();//获取当前音乐
    ui->label_musicName->setText(musicFilses[index]);//显示歌名
}

void MainWindow::on_comboBox_currentIndexChanged(int index)
{
    switch(index)
    {
        case 0:
            playlist->setPlaybackMode(QMediaPlaylist::Sequential);
            break;
        case 1:
            playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
            break;
        case 2:
            playlist->setPlaybackMode(QMediaPlaylist::Random);
            break;
        default:
            playlist->setPlaybackMode(QMediaPlaylist::Sequential);
            break;
    }

}

void MainWindow::on_playlistIndexChanged(int index)
{
    if (index >= 0 && index < musicFilses.size()) {
        ui->label_musicName->setText(musicFilses[index]);
    }
}
